<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
 */

require("includes/application_top.php");

if ($cart->count_contents() > 0) {
    include(DIR_WS_CLASSES . 'payment.php');
    $payment_modules = new payment;
}

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_SHOPPING_CART);

$breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_SHOPPING_CART));

require(DIR_WS_INCLUDES . 'template_top.php');
?>

<h1><?php echo HEADING_TITLE; ?></h1>

<?php
if ($cart->count_contents() > 0) {
    ?>

    <?php //echo tep_draw_form('cart_quantity', tep_href_link(FILENAME_SHOPPING_CART, 'action=update_product')); ?>

    <div class="contentContainer">
        <h2><?php echo TABLE_HEADING_PRODUCTS; ?></h2>

        <div class="contentText">

            <?php
            $any_out_of_stock = 0;
            $products = $cart->get_products();
            for ($i = 0, $n = sizeof($products); $i < $n; $i++) {
// Push all attributes information in an array
                if (isset($products[$i]['attributes']) && is_array($products[$i]['attributes'])) {
                    while (list($option, $value) = each($products[$i]['attributes'])) {
                        echo tep_draw_hidden_field('id[' . $products[$i]['id'] . '][' . $option . ']', $value);
                        $attributes = tep_db_query("select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix
                                      from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_OPTIONS_VALUES . " poval, " . TABLE_PRODUCTS_ATTRIBUTES . " pa
                                      where pa.products_id = '" . (int) $products[$i]['id'] . "'
                                       and pa.options_id = '" . (int) $option . "'
                                       and pa.options_id = popt.products_options_id
                                       and pa.options_values_id = '" . (int) $value . "'
                                       and pa.options_values_id = poval.products_options_values_id
                                       and popt.language_id = '" . (int) $languages_id . "'
                                       and poval.language_id = '" . (int) $languages_id . "'");
                        $attributes_values = tep_db_fetch_array($attributes);

                        $products[$i][$option]['products_options_name'] = $attributes_values['products_options_name'];
                        $products[$i][$option]['options_values_id'] = $value;
                        $products[$i][$option]['products_options_values_name'] = $attributes_values['products_options_values_name'];
                        $products[$i][$option]['options_values_price'] = $attributes_values['options_values_price'];
                        $products[$i][$option]['price_prefix'] = $attributes_values['price_prefix'];
                    }
                }
            }
            ?>

            <table id="booking-list-tbl" border="0" width="100%" cellspacing="0" cellpadding="0">
                <thead>
                <td>Thứ tự</td>
                <td>Ảnh</td>
                <td>Tên sản phẩm</td>
                <td>Giá tiền</td>
                <td>Số lượng</td>
                <td>Thành tiền</td>
                </thead>
                <tbody>

                    <?php
                    //var_dump($products);
                    $i = 0;
                    foreach ($products as $product) {
                        $i++;
                        ?>
                        <tr>
                            <td>
                                <?php echo $i ?>
                            </td>
                            <td>
                                <img height="100px" width="100px" src="<?php echo HTTP_SERVER . '/catalog/images/' . $product['image'] ?>">

                            </td>
                            <td>
                                <?php echo $product['name'] ?>
                            </td>
                            <td>
                                <?php echo $product['price'] ?>
                            </td>
                            <td>
                                <input type="text" maxlength="2" size="2" name="amount"/>
                            </td>
                            <td>
                                <input type="text" name="total" size="4" readonly="readonly"/>
                            </td>


                        </tr>
                        <?php
                    }
                    //                for ($i = 0, $n = sizeof($products); $i < $n; $i++) {
                    //                    echo '      <tr>';
                    //
                        //                    $products_name = '<table border="0" cellspacing="2" cellpadding="2">' .
                    //                            '  <tr>' .
                    //                            '    <td align="center"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '">' . tep_image(DIR_WS_IMAGES . $products[$i]['image'], $products[$i]['name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a></td>' .
                    //                            '    <td valign="top"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '"><strong>' . $products[$i]['name'] . '</strong></a>';
                    //
                        //                    if (STOCK_CHECK == 'true') {
                    //                        $stock_check = tep_check_stock($products[$i]['id'], $products[$i]['quantity']);
                    //                        if (tep_not_null($stock_check)) {
                    //                            $any_out_of_stock = 1;
                    //
                        //                            $products_name .= $stock_check;
                    //                        }
                    //                    }
                    //
                        //                    if (isset($products[$i]['attributes']) && is_array($products[$i]['attributes'])) {
                    //                        reset($products[$i]['attributes']);
                    //                        while (list($option, $value) = each($products[$i]['attributes'])) {
                    //                            $products_name .= '<br /><small><i> - ' . $products[$i][$option]['products_options_name'] . ' ' . $products[$i][$option]['products_options_values_name'] . '</i></small>';
                    //                        }
                    //                    }
                    //
                        //                    $products_name .= '<br /><br />' . tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], 'size="4"') . tep_draw_hidden_field('products_id[]', $products[$i]['id']) . tep_draw_button(IMAGE_BUTTON_UPDATE, 'refresh') . '&nbsp;&nbsp;&nbsp;' . TEXT_OR . '<a href="' . tep_href_link(FILENAME_SHOPPING_CART, 'products_id=' . $products[$i]['id'] . '&action=remove_product') . '">' . TEXT_REMOVE . '</a>';
                    //
                        //                    $products_name .= '    </td>' .
                    //                            '  </tr>' .
                    //                            '</table>';
                    //
                        //                    echo '        <td valign="top">' . $products_name . '</td>' .
                    //                    '        <td align="right" valign="top"><strong>' . $currencies->display_price($products[$i]['final_price'], tep_get_tax_rate($products[$i]['tax_class_id']), $products[$i]['quantity']) . '</strong></td>' .
                    //                    '      </tr>';
                    //                }
                    ?>
                </tbody>
            </table>

            <p align="right"><strong><?php echo SUB_TITLE_SUB_TOTAL; ?> <?php echo $currencies->format($cart->show_total()); ?></strong></p>

            <?php
            if ($any_out_of_stock == 1) {
                if (STOCK_ALLOW_CHECKOUT == 'true') {
                    ?>

                    <p class="stockWarning" align="center"><?php echo OUT_OF_STOCK_CAN_CHECKOUT; ?></p>

                    <?php
                } else {
                    ?>

                    <p class="stockWarning" align="center"><?php echo OUT_OF_STOCK_CANT_CHECKOUT; ?></p>

                    <?php
                }
            }
            ?>

        </div>

        <div class="buttonSet">
            <span class="buttonAction">
                <?php //echo tep_draw_button(IMAGE_BUTTON_CHECKOUT, 'triangle-1-e', tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'), 'primary');   ?>
                <?php //echo tep_draw_button('Đặt hàng', 'triangle-1-e', '', 'primary'); ?>
                <span class=""><a href="javascript:void(0)" id="online-book-link" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary ui-priority-primary" role="button" aria-disabled="false"><span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-e"></span><span class="ui-button-text">Đặt hàng</span></a></span>
            </span>
        </div>
        <br/><br/><br/><br/><br/>
        <script type="text/javascript" src="<?php echo HTTP_SERVER . '/catalog/ext/jquery/jquery-1.11.1.js' ?>"></script>
        <script type="text/javascript" src="<?php echo HTTP_SERVER . '/catalog/ext/jquery/validate/jquery.validate.js' ?>"></script>
        <div id="book-form" style="display: none">


            <style type="text/css">
                .error {
                    color: red;
                    padding-left:3px;
                }

            </style>
            <form class="cmxform" id="commentForm" method="post" action="">
                <table cellspacing="0" cellpadding="2" border="0" width="100%">

                    <tbody><tr>
                            <td class="fieldKey">Họ và tên:<font color="red">*</font></td>
                            <td class="fieldValue"><input type="text" name="name" id="name"></td>
                        </tr>
                        <tr>
                            <td class="fieldKey">Email:<font color="red">*</font></td>
                            <td class="fieldValue"><input type="text" name="email_address" id="email_address"></td>
                        </tr>
                        <tr>
                            <td class="fieldKey">Địa chỉ nhận hàng:</td>
                            <td class="fieldValue"><input type="text" name="shipping_address"></td>
                        </tr>
                        <tr>
                            <td class="fieldKey">Số điện thoại:<font color="red">*</font></td>
                            <td class="fieldValue"><input type="text" name="phone" id="phone"></td>
                        </tr>
                        <tr>
                            <td class="fieldKey">Thông tin thêm:</td>
                            <td class="fieldValue">
                                <textarea name="desc"></textarea>
                            </td>
                        </tr>
                    </tbody>



                </table>
                <p align="left">
                <div class="buttonSet">

                    <span class="buttonAction">

                        <input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary ui-priority-primary" type="submit" value="Hoàn tất">
                    </span>
                </div>

                </p>
            </form>








        </div>

        <script type="text/javascript" src="<?php echo HTTP_SERVER . '/catalog/ext/jquery/DataTables-1.10.4/media/js/jquery.dataTables.js' ?>"></script>
        <link href="<?php echo HTTP_SERVER . '/catalog/ext/jquery/DataTables-1.10.4/media/css/jquery.dataTables.css' ?>" rel="stylesheet">
        <script type="text/javascript">
            $.validator.setDefaults({
                submitHandler: function () {
                    alert("submitted!");
                }
            });
            $(document).ready(function () {


                // validate the comment form when it is submitted
                // validate signup form on keyup and submit
                $("#commentForm").validate({
                    rules: {
                        name: {
                            required: true,
                            minlength: 2
                        },
                        email_address: {
                            required: true,
                            email: true
                        },
                        phone: "required"
                    },
                    messages: {
                        phone: "Bạn chưa nhập số điện thoại",
                        lastname: "Please enter your lastname",
                        name: {
                            required: "Hãy nhập tên của bạn",
                            minlength: "Tên phải có ít nhất 2 chữ cái"
                        },
                        email_address: "Email bạn điền chưa đúng"
                    }
                });


                $("#online-book-link").click(function () {
                    $("#book-form").show();
                });


                var table = $('#booking-list-tbl').DataTable({
                    bFilter: false,
                    bInfo: false,
                    bPaginate: false,
                    "oLanguage": {
                        "sProcessing": "Đang xử lý...",
                        "sLengthMenu": "Xem _MENU_ mục",
                        "sZeroRecords": "Không tìm thấy dòng nào phù hợp",
                        "sInfo": "Đang xem _START_ đến _END_ trong tổng số _TOTAL_ mục",
                        "sInfoEmpty": "Đang xem 0 đến 0 trong tổng số 0 mục",
                        "sInfoFiltered": "(được lọc từ _MAX_ mục)",
                        "sInfoPostFix": "",
                        "sSearch": "Tìm:",
                        "sUrl": "",
                        "oPaginate": {
                            "sFirst": "Đầu",
                            "sPrevious": "Trước",
                            "sNext": "Tiếp",
                            "sLast": "Cuối"
                        }
                    }
                });


            });
        </script>


        <?php
        $initialize_checkout_methods = $payment_modules->checkout_initialization_method();

        if (!empty($initialize_checkout_methods)) {
            ?>

                                                                                                                                                                                <!--<p align="right" style="clear: both; padding: 15px 50px 0 0;"><?php echo TEXT_ALTERNATIVE_CHECKOUT_METHODS; ?></p>-->

            <?php
            reset($initialize_checkout_methods);
            while (list(, $value) = each($initialize_checkout_methods)) {
                ?>

                                                                                                                                                                                                                                                                    <!--<p align="right"><?php echo $value; ?></p>-->

                <?php
            }
        }
        ?>

    </div>

    <!--</form>-->

    <?php
} else {
    ?>

    <div class="contentContainer">
        <div class="contentText">
            <?php echo TEXT_CART_EMPTY; ?>

            <p align="right"><?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', tep_href_link(FILENAME_DEFAULT)); ?></p>
        </div>
    </div>

    <?php
}

require(DIR_WS_INCLUDES . 'template_bottom.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
